package sort;

/**
 * Created with IntelliJ IDEA.
 * Description: 直接插入排序
 * User: Li_yizYa
 * Date: 2025/9/8
 * Time: 21:46
 */
public class InsertSort {
    private static void insertSort(int[] nums) {
        if (nums == null || nums.length == 0) {
            return;
        }
        for (int i = 1; i < nums.length; i++) {
            int j = i - 1;
            int tmp = nums[i];
            for (; j >= 0; j--) {
                if (tmp < nums[j]) {
                    nums[j + 1] = nums[j];
                } else {
                    break;
                }
            }
            nums[j + 1] = tmp;
        }
    }
    public static void main(String[] args) {
        int[] nums = {6,5,4,4,3,1,2,10,8};
        insertSort(nums);
        for (int num : nums) {
            System.out.print(num + " ");
        }
    }
//    private static int[] insertSort(int[] nums) {
//        for (int i = 1; i < nums.length; i++) { // 待排序列
//            int j = i - 1;
//            int tmp = nums[i];
//            for (; j >= 0 && tmp < nums[j]; j--) {
//                nums[j + 1] = nums[j];
//            }
//            nums[j + 1] = tmp;
//        }
//        return nums;
//    }
//
//    public static void main(String[] args) {
//        int[] ret = insertSort(new int[]{6,5,4,4,3,1,2,10,8});
//        for (int num : ret) {
//            System.out.print(num + " ");
//        }
//    }
}
